package cae.logic.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


public class CaeClipConfiguration {

	private static final String CLIP_PROPERTIES = "clip.properties";
	public static final String PORT = "port";
	public static final String AUTO_MESSAGES = "autoMessages";
	public static final String TIME_BETWEEN_MESSAGES = "timeBetweenAutoMessages";
	public static final String FILE_PORT = "filePort";
	public static final String SOUND_ENABLED = "soundEnabled";
	public static final String TEME_BATWEEN_DESTINATION_TEST = "timeBetweenDestinationTest";
	public static final String ICON_FILE = "iconFile";
	public static final String POPUP_SOUND = "popupSound";
	public static final String PORT_CHAT = "portChat";
	public static final String MAIL_ICON = "messageIcon";
	public static final String URL_DEST_UPDATE = "destUpdateUrl";
	
	private static Logger logger = Logger.getLogger(CaeClipConfiguration.class);
	private static CaeClipConfiguration instance;
	private Properties prop;
	
	private CaeClipConfiguration()
	{
		load();
	}
	
	public static CaeClipConfiguration getInstance()
	{
		if(instance == null)
			instance = new CaeClipConfiguration();
		return instance;
	}
	
	public String getSProp(String key)
	{
		return (String) prop.get(key);
	}
	
	public int getIProp(String key)
	{
		return Integer.valueOf((String)prop.get(key)).intValue();
	}
	
	public void load()
	{
		prop = new Properties();
		try {
			InputStream is = new FileInputStream(CLIP_PROPERTIES);
			prop.load(is);
			is.close();
		} catch (Exception e)
		{
			logger.fatal("No se puede encontrar el archivo de configuracion " + CLIP_PROPERTIES);
			new RuntimeException("ERROR DE CONFIGURACION");
		}
	}
	
	public void setSProp(String key, String value)
	{
		prop.setProperty(key, value);
		save();
	}
	
	public void setIProp(String key, int value)
	{
		prop.setProperty(key, Integer.toString(value));
		save();
	}

	private void save() {
		try {
			OutputStream os = new FileOutputStream(CLIP_PROPERTIES);
			prop.store(os, "");
			os.close();
		} catch (Exception e)
		{
			logger.fatal("No se puede escribir el archivo de configuracion " + CLIP_PROPERTIES);
			new RuntimeException("ERROR DE CONFIGURACION");
		}
	}
	
	
}
